<?php
namespace Home\Biz;

/**
 *
 * @author Xuyafei
 *        
 */
class AuthBiz {
	
	
	/**
	 * 请注意，这是ID
	 * @param unknown $user_id
	 */
	public static function addBussinessAuth($user_id){
		self::addToGroupRelationUser(C('AuthBussinessGroupConfig.group_id'), $user_id);
	}
	
	
	
	public static function addVRBussinessAuth($user_id){
		self::addToGroupRelationUser(C('AuthVRBussinessGroupConfig.group_id'), $user_id);
	}
	
	public static function addSTBussinessAuth($user_id){
		self::addToGroupRelationUser(C('AuthSTBussinessGroupConfig.group_id'), $user_id);
	}
	/**
	 * 用户组添加页面
	 * @param unknown $group_id
	 * @param unknown $menu_relation
	 */
	public static function addToGroupRelationUser($group_id,$user_relation){
		$user_ids=explode(",",$user_relation);
		 
		$user_group_en=M('user_group','','DB_whaley_system')->where("group_id = '$group_id'")->find();
		
		/**
		 * 查询这个组的所有页面
		 * @var Ambiguous $lst_group_user_relation
		 */
		$lst_group_menu_relation = M('group_resouce_relation','','DB_whaley_system')->where("group_id='$group_id'")->select();
		
		foreach ($user_ids as $user_id){
			
			/**
			 * 先查看这个权限组有没有这个用户
			 */
			$group_user_en_check= M('group_user_relation','','DB_whaley_system')	->where("group_id = '$group_id' and user_id ='$user_id' ")->find();
				
			$whaley_user_en=M('whaley_user','','DB_whaley_system')->where("user_id = '$user_id'")->find();
				
			/**
			 * 如果不存在，将这个组的所有页面权限赋值给这个用户
			 */
			if($group_user_en_check ==null){
				
				
				foreach ($lst_group_menu_relation as $group_menu_relation){
					$menu_id=$group_menu_relation['menu_id'];
					
					 self::checkExistsRule($user_id,$menu_id);
					
					if(!self::checkExistsRule($user_id,$menu_id)){
					$menu_resource_en=M('menu_resource','','DB_whaley_system')->where("menu_id = '$menu_id'")->find();
					$user_rule_en = array(
							'rule_id'     =>uniqid(),
							'user_id'     =>$user_id,
							'user_name'   =>$whaley_user_en['user_name'],
							'menu_id'     =>$menu_id,
							'level'       =>$menu_resource_en['level'],
							'menu_domain' =>$menu_resource_en['menu_domain'],
							'menu_domain_desc'=>$menu_resource_en['menu_domain_desc'],
							'default_url'     =>$menu_resource_en['default_url']
					);
					 M('user_rule','','DB_whaley_system')->add($user_rule_en);
				  }
				}
				
				$group_user_relation_en = array(
						'relation_id'	=> uniqid(),
						'group_id'      =>$group_id,
						'group_name'	=> $user_group_en['group_name'],
						'user_id'       =>$whaley_user_en['user_id'],
						'user_name'       =>$whaley_user_en['user_name'],
						'create_date'	=> date("y-m-d H:i:s", time())
				);
				M('group_user_relation','','DB_whaley_system')->add($group_user_relation_en);
			}
		}
		
		
	}
	
	public static function checkExistsRule($user_id,$menu_id){
		$user_rule_en = M('user_rule','','DB_whaley_system')->where("user_id = '$user_id' and menu_id = '$menu_id'")->find();
		if($user_rule_en !=null){
			return true;
		}
		return false;
	}
	
	
	
} 